home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / sed-2_00.lha / sed-2.00 / utils.c < prev   
C/C++ Source or Header  |  1993-07-14  |  5KB  |  325 lines

  1. /*  Functions from hack's utils library.
  2.     Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* These routines were written as part of a library (by hack), but since most
  19.    people don't have the library, here they are.  */
  20.  
  21. #ifdef __STDC__
  22. #define VOID void
  23. #else
  24. #define VOID char
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #if HAVE_STRING_H || defined(STDC_HEADERS)
  29. #include <string.h>
  30. #else
  31. #include <strings.h>
  32. #endif
  33. #if defined(STDC_HEADERS)
  34. #include <stdlib.h>
  35. #else
  36. #ifdef RX_MEMDBUG
  37. #include <sys/types.h>
  38. #include <malloc.h>
  39. #else
  40. VOID *malloc();
  41. VOID *realloc();
  42. #endif /* ndef RX_MEMDBUG  */
  43. #endif
  44.  
  45. VOID *ck_malloc();
  46.  
  47. char *myname;
  48.  
  49.  
  50. #ifdef __STDC__
  51. #include <stdarg.h>
  52.  
  53. /* Print an error message and exit */
  54. void
  55. panic(char *str, ...)
  56. {
  57.     va_list iggy;
  58.  
  59.     fprintf(stderr,"%s: ",myname);
  60.     va_start(iggy,str);
  61. #ifdef HAVE_VPRINTF
  62.     vfprintf(stderr,str,iggy);
  63. #else
  64. #ifdef HAVE_DOPRNT
  65.     _doprnt(str,&iggy,stderr);
  66. #endif
  67. #endif
  68.     va_end(iggy);
  69.     putc('\n',stderr);
  70.     exit(4);
  71. }
  72.  
  73. #else
  74. #include <varargs.h>
  75.  
  76. void
  77. panic(str,va_alist)
  78. char *str;
  79. va_dcl
  80. {
  81.     va_list iggy;
  82.  
  83.     fprintf(stderr,"%s: ",myname);
  84.     va_start(iggy);
  85. #ifdef HAVE_VPRINTF
  86.     vfprintf(stderr,str,iggy);
  87. #else
  88. #ifdef HAVE_DOPRNT
  89.     _doprnt(str,&iggy,stderr);
  90. #endif
  91. #endif
  92.     va_end(iggy);
  93.     putc('\n',stderr);
  94.     exit(4);
  95. }
  96.  
  97. #endif
  98.  
  99. /* Store information about files opened with ck_fopen
  100.    so that error messages from ck_fread, etc can print the
  101.    name of the file that had the error */
  102. #define N_FILE 32
  103.  
  104. struct id {
  105.     FILE *fp;
  106.     char *name;
  107. };
  108.  
  109. static struct id __id_s[N_FILE];
  110.  
  111. /* Internal routine to get a filename from __id_s */
  112. char *
  113. __fp_name(fp)
  114. FILE *fp;
  115. {
  116.     int n;
  117.  
  118.     for(n=0;n<N_FILE;n++) {
  119.         if(__id_s[n].fp==fp)
  120.             return __id_s[n].name;
  121.     }
  122.     return "{Unknown file pointer}";
  123. }
  124.  
  125. /* Panic on failing fopen */
  126. FILE *
  127. ck_fopen(name,mode)
  128. char *name;
  129. char *mode;
  130. {
  131.     FILE    *ret;
  132.     int    n;
  133.  
  134.     ret=fopen(name,mode);
  135.     if(ret==(FILE *)0)
  136.         panic("Couldn't open file %s",name);
  137.     for(n=0;n<N_FILE;n++) {
  138.         if(ret==__id_s[n].fp) {
  139.             free((VOID *)__id_s[n].name);
  140.             __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
  141.             strcpy(__id_s[n].name,name);
  142.             break;
  143.         }
  144.     }
  145.     if(n==N_FILE) {
  146.         for(n=0;n<N_FILE;n++)
  147.             if(__id_s[n].fp==(FILE *)0)
  148.                 break;
  149.         if(n==N_FILE)
  150.             panic("Internal error: too many files open");
  151.         __id_s[n].fp=ret;
  152.         __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
  153.         strcpy(__id_s[n].name,name);
  154.     }
  155.     return ret;
  156. }
  157.  
  158. /* Panic on failing fwrite */
  159. void
  160. ck_fwrite(ptr,size,nmemb,stream)
  161. char *ptr;
  162. int size,nmemb;
  163. FILE *stream;
  164. {
  165.     if(fwrite(ptr,size,nmemb,stream)!=nmemb)
  166.         panic("couldn't write %d items to %s",nmemb,__fp_name(stream));
  167. }
  168.  
  169. /* Panic on failing fclose */
  170. void
  171. ck_fclose(stream)
  172. FILE *stream;
  173. {
  174.     if(fclose(stream)==EOF)
  175.         panic("Couldn't close %s",__fp_name(stream));
  176. }
  177.  
  178. /* Panic on failing malloc */
  179. VOID *
  180. ck_malloc(size)
  181. int size;
  182. {
  183.     VOID *ret;
  184.  
  185.     if(!size)
  186.         size++;
  187.     ret=malloc(size);
  188.     if(ret==(VOID *)0)
  189.         panic("Couldn't allocate memory");
  190.     return ret;
  191. }
  192.  
  193. /* Panic on failing malloc */
  194. VOID *
  195. xmalloc(size)
  196. int size;
  197. {
  198.   return ck_malloc (size);
  199. }
  200.  
  201. /* Panic on failing realloc */
  202. VOID *
  203. ck_realloc(ptr,size)
  204. VOID *ptr;
  205. int size;
  206. {
  207.     VOID *ret;
  208.  
  209.     if (!ptr)
  210.       return ck_malloc (size);
  211.     ret=realloc(ptr,size);
  212.     if(ret==(VOID *)0)
  213.         panic("Couldn't re-allocate memory");
  214.     return ret;
  215. }
  216.  
  217. /* Return a malloc()'d copy of a string */
  218. char *
  219. ck_strdup(str)
  220. char *str;
  221. {
  222.     char *ret;
  223.  
  224.     ret=(char *)ck_malloc(strlen(str)+2);
  225.     strcpy(ret,str);
  226.     return ret;
  227. }
  228.  
  229.  
  230. /* Implement a variable sized buffer of 'stuff'.  We don't know what it is,
  231.    nor do we care, as long as it doesn't mind being aligned by malloc. */
  232.  
  233. struct buffer {
  234.     int    allocated;
  235.     int    length;
  236.     char    *b;
  237. };
  238.  
  239. #define MIN_ALLOCATE 50
  240.  
  241. VOID *
  242. init_buffer()
  243. {
  244.     struct buffer *b;
  245.  
  246.     b=(struct buffer *)ck_malloc(sizeof(struct buffer));
  247.     b->allocated=MIN_ALLOCATE;
  248.     b->b=(char *)ck_malloc(MIN_ALLOCATE);
  249.     b->length=0;
  250.     return (VOID *)b;
  251. }
  252.  
  253. void
  254. flush_buffer(bb)
  255. VOID *bb;
  256. {
  257.     struct buffer *b;
  258.  
  259.     b=(struct buffer *)bb;
  260.     free(b->b);
  261.     b->b=0;
  262.     b->allocated=0;
  263.     b->length=0;
  264.     free(b);
  265. }
  266.  
  267. int
  268. size_buffer(b)
  269. VOID *b;
  270. {
  271.     struct buffer *bb;
  272.  
  273.     bb=(struct buffer *)b;
  274.     return bb->length;
  275. }
  276.  
  277. void
  278. add_buffer(bb,p,n)
  279. VOID *bb;
  280. char *p;
  281. int n;
  282. {
  283.     struct buffer *b;
  284.     int x;
  285.     char * cp;
  286.  
  287.     b=(struct buffer *)bb;
  288.     if(b->length+n>b->allocated) {
  289.         b->allocated = (b->length + n) * 2;
  290.         b->b=(char *)ck_realloc(b->b,b->allocated);
  291.     }
  292.     
  293.     x = n;
  294.     cp = b->b + b->length;
  295.     while (x--)
  296.       *cp++ = *p++;
  297.     b->length+=n;
  298. }
  299.  
  300. void
  301. add1_buffer(bb,ch)
  302. VOID *bb;
  303. int ch;
  304. {
  305.     struct buffer *b;
  306.  
  307.     b=(struct buffer *)bb;
  308.     if(b->length+1>b->allocated) {
  309.         b->allocated*=2;
  310.         b->b=(char *)ck_realloc(b->b,b->allocated);
  311.     }
  312.     b->b[b->length]=ch;
  313.     b->length++;
  314. }
  315.  
  316. char *
  317. get_buffer(bb)
  318. VOID *bb;
  319. {
  320.     struct buffer *b;
  321.  
  322.     b=(struct buffer *)bb;
  323.     return b->b;
  324. }
  325.